using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Xna.Framework.Graphics;
using Itdavany.Objects;
using XnaColor = Microsoft.Xna.Framework.Graphics.Color;
using SystemColor = System.Drawing.Color;
using Itdavany.Utility;
using Itdavany.Data;
using System.IO;
using Microsoft.Xna.Framework.Content;
using Itdavany.Data.XmlFormat;

namespace ItdavanyViewer
{
    public partial class ItdavanyView : ItdavanyViewer.XNAView
    {
        private BaseObject obj = null;
        private string rootDir = null;
        private ContentManager contentMgr = null;
        private XnaColor bgColor = XnaColor.White;
        
        public ItdavanyView() : base()
        {
            InitializeComponent();
        }

        public ItdavanyView(string root)
            : this()
        {
            RootDirectory = root;
        }

        public BaseObject Object
        {
            get
            {                
                return this.obj;
            }

            set
            {
                this.obj = value;
            }
        }

        public override void Draw()
        {
            this.graphics.GraphicsDevice.Clear(bgColor);

            if(obj != null)
                obj.Draw();

            base.Draw();
        }

        public override void XnaUpdate(Microsoft.Xna.Framework.GameTime gameTime)
        {
            if (obj != null)
            {
                //if (obj.GetType() == typeof(AnimationClip))
                    obj.Update(gameTime);
                //else
                    //obj.Update(gameTime);
            }

            base.XnaUpdate(gameTime);
        }

        public override string RootDirectory
        {
            get
            {
                return rootDir;
            }
            set
            {
                if (value != rootDir)
                {
                    rootDir = value;
                    if (contentMgr != null)
                    {
                        contentMgr.Unload();
                        contentMgr.Dispose();
                        contentMgr = null;
                    }

                    if (rootDir != null)
                    {
                        contentMgr = new ContentManager(this, rootDir);
                        Shared.Initialize(contentMgr, graphics);
                        Shared.ObjectDescriber.SetTypeMatching(
                            ObjectType.Button, typeof(ItdavanyObject));
                        Shared.ObjectDescriber.SetTypeMatching(
                            ObjectType.SelectBox, typeof(ItdavanyObject));
                        Shared.ObjectDescriber.SetTypeMatching(
                            ObjectType.Dialog, typeof(ItdavanyObject));
                        Shared.ObjectDescriber.SetTypeMatching(
                            ObjectType.ShowBox, typeof(ItdavanyObject));
                        Shared.ObjectDescriber.SetTypeMatching(
                            ObjectType.TextViewer, typeof(ItdavanyObject));
                        Shared.ObjectDescriber.SetTypeMatching(
                            ObjectType.StatBox, typeof(ItdavanyObject));
                    }
                }
            }
        }

        public bool Read(string path)
        {            
            string name = Path.Combine(
                Path.GetDirectoryName(path),
                Path.GetFileNameWithoutExtension(path));
            string ext = Path.GetExtension(path);
                        
            switch (ext)
            {
                case ".iod":
                    Shared.ObjectDescriber.ClearObjectCache();
                    switch (Shared.ObjectDescriber.GetObjectType(name))
                    {
                        case ObjectType.Animation:
                        //case ObjectType.AnimationClip:
                            Object = new AnimationClip(Shared.ObjectDescriber.GetObject<Animation>(name));
                            break;
                        default:
                            Object = Shared.ObjectDescriber.GetObject<ItdavanyObject>(name);
                            break;
                    }
                    
                    break;
                case ".xnb":
                    Object = new TexturePanel(name);
                    break;
                default:
                    return false;
            }

            return true;
        }

        public XnaColor BackgroundXnaColor
        {
            get
            {
                return bgColor;
            }
            set
            {
                bgColor = value;
            }
        }

        public SystemColor BackgroundColor
        {
            get
            {
                return SystemColor.FromArgb(
                    bgColor.R, bgColor.G, bgColor.B);
            }
            set
            {
                bgColor = new XnaColor(
                    value.R, value.G, value.B);
            }
        }

        public void Close()
        {
            Object = null;
        }

        public class ItdavanyObject : Widget
        {
            private string frame;
            
            public ItdavanyObject(FrameArray frmArr) : base(frmArr)
            {
                CurrentFrame = null;
                if(Frames.Count > 0)
                    CurrentFrame = Frames[0];
            }

            public IList<string> Frames
            {
                get
                {
                    return new List<string>(this.FrameData.Keys);                    
                }
            }

            public string CurrentFrame
            {
                set
                {
                    this.frame = value;
                    if (this.frame != null)
                        this.SubScene = FrameData[this.frame];
                    else
                        this.SubScene = null;
                }
                get
                {
                    return this.frame;
                }
            }
        }
    }
}

